# -*- coding: utf-8 -*-
'''
Created on 2018年6月17日
@author: liaolzl
'''
#1.异常处理
from urllib import request,error
try:
    response = request.urlopen('http://cuiqingcai.com/index.html')
except error.URLError as e:
        print(e.reason)

#2.HTTPError是URLError的子类
from urllib import request,error
try:
    response = request.urlopen('http://cuiqingcai.com/index.html')
except error.HTTPError as e:
    print(e.reason,e.code,e.headers,sep='\n')
except error.URLError as e:
    print(e.reason)
else:
    print('Request Successfully') 

#3.timeout异常
import urllib.request
import socket
import urllib.error
try:
    response = urllib.request.urlopen('https://www.baidu.com',timeout = 0.01)
except urllib.error.URLError as e:
    print(type(e.reason))
    if isinstance(e.reason, socket.timeout):
        print('TIME OUT') 